package com.doudou.dubbo.demo;

import com.doudou.dubbo.demo.api.GreetingService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.RpcContext;

/**
 * @author zhaozw
 */
public class ApiConsumerMock {

    public static void main(String[] args) throws InterruptedException {
        ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer"));
        referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        referenceConfig.setInterface(GreetingService.class);
        referenceConfig.setTimeout(5000);
        referenceConfig.setVersion("1.0.0");
        referenceConfig.setGroup("dubbo");
        // 设置服务启动时不检查服务提供者是否可以
        referenceConfig.setCheck(false);
        // 设置服务调用时使用mock
        // 先调用远程服务，远程服务调用失败（比如服务不存在）时，降级执行mock功能
        referenceConfig.setMock(true);
        Thread.sleep(1000);
        System.out.println("===================================>>>> " + System.currentTimeMillis());
        GreetingService greetingService = referenceConfig.get();
        RpcContext.getContext().setAttachment("company", "juzi");
        Thread.sleep(1000);
        System.out.println("===================================>>>> " + System.currentTimeMillis());
        String result = greetingService.sayHello("result");
        System.out.println("result : " + result);
    }

}
